package com.share.wechat.fristpage.controller;

import com.share.wechat.fristpage.entity.QiHuEntity;
import com.share.wechat.fristpage.service.AnswerService;
import com.share.wechat.fristpage.service.QiHuService;
import com.share.wechat.fristpage.vo.ReturnResult;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

/**
 * 最近一段时间玩的答题神奇的东西 慢慢的写哈
 * Created by yujl on 2018/1/16.
 */
@Controller
@RequestMapping("/dati/")
public class AnswerController {
    private Logger logger = Logger.getLogger(this.getClass());
    /**
     * 默认是否进行答案搜索
     */
    private final static String SEARCH_ANSWER_YES = "1";

    @Resource
    private AnswerService answerService;
    @Resource
    private QiHuService qiHuService;

    @RequestMapping("selectModel")
    public String selectModel(Model model,@RequestParam(name = "type",defaultValue = "1") String searchType,@RequestParam(name = "way",defaultValue = "cddh") String searchWay) throws Exception {
        ReturnResult result = new ReturnResult();
        String keyWord = null;
        List<String> choices = new ArrayList<>(0);
        Map<String, Object> task = new HashMap<>();
        String json = qiHuService.doGetQiHu(searchWay);
        List<QiHuEntity> qihuLists = qiHuService.dealAnswer(json);
        for (QiHuEntity entity : qihuLists) {
            if (entity == null) {
                entity = new QiHuEntity();
            }
            if (StringUtils.isEmpty(entity.getTitle())) {
                entity.setTitle("wanter君没有分析出来哎!");
            }
            if (CollectionUtils.isEmpty(entity.getAnswers())) {
                List<String> answer = new ArrayList<>(3);
                answer.add("分析出错1!");
                answer.add("分析出错2!");
                answer.add("分析出错3!");
                entity.setAnswers(answer);
            }
            if (StringUtils.isEmpty(entity.getResult())) {
                entity.setResult("凭运气");

            }

        }


        task.put("qihu", qihuLists);//设置360分析出来的结果
        if (qihuLists.size() > 1) {
            choices = qihuLists.get(1).getAnswers();
            keyWord = qihuLists.get(1).getTitle();
        }
        Map<String, List<String>> searchMap = new LinkedHashMap<>(5);
        List<String> answerList = answerService.searchOnBaiduiEasy(keyWord, choices).get(keyWord);
        searchMap.put("问题!", answerList);
        //根据答案去搜索结果
        if (SEARCH_ANSWER_YES.equals(searchType)){
            for (String choice : choices) {
                List<String> oneList = new ArrayList<>(1);
                oneList.add(choice);
                List<String> choiceList = answerService.searchOnBaiduiEasy(choice, oneList).get(choice);
                searchMap.put(choice, choiceList);
            }
        }

        logger.info("百度搜索的结果为" + searchMap);
        task.put("baidu", searchMap);//设置百度搜索出来的数据
        boolean start = true;
        logger.debug("开始玩冲顶大会咯");
        //先判断冲顶大会是否开始了
        if (start) {
            result.setCode(200);
            result.setMsg("欢迎答题 祝你好运哦");
            result.setData(task);
        } else {
            result.setCode(-100);
            result.setMsg("等待银出题中ing.....");
        }
        //model.addAttribute("code","-1"); //遇到未知错误
        logger.error("目前的答案分析出来为.....");

        logger.error(task);

        model.addAttribute("result", result);
        return "/html/app-show/search";
    }

    /**
     * 仅仅支撑前段框框的搜索的东西
     *
     * @param keyWord 关键词
     * @return json
     */
    @RequestMapping("search/ByKeyWord")
    @ResponseBody
    public ReturnResult searchKeyWord(@RequestParam(name = "key", defaultValue = "json") String keyWord) {
        ReturnResult result = new ReturnResult();

        try {
            result.setMsg("查询成功");
            logger.info("查询的关键词为" + keyWord);
            List<Map<String, List<String>>> data = answerService.search(keyWord);
            logger.info("查询出的数据为" + data);
            result.setData(data);
        } catch (Exception e) {
            logger.error("查询后台的关键词的时候报错!!!");
            logger.error(e.getLocalizedMessage());
            logger.error(e.getStackTrace());
            result.setData("服务器查询失败!请联系管理员");
            result.setCode(500);
            result.setMsg(e.getMessage());
        }
        return result;

    }

    @RequestMapping("search/play")
    @ResponseBody

    public List<QiHuEntity> getByQiHu(@RequestParam(name = "way", defaultValue = "chongdindahui", required = false) String way) throws Exception {
        String json = qiHuService.doGetQiHu(QiHuService.CHONG_DIN_DA_HUI);
        List<QiHuEntity> list = qiHuService.dealAnswer(json);
        logger.info("后台返回的数据为" + list);

        return list;
    }


}
